//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

//====================================================
// Created     : 2004-1-6
// Author      : FengTao
// Description : Header file for testBsp of x86
//====================================================
#ifndef __ELASTOS_TESTX86_H__
#define __ELASTOS_TESTX86_H__

// number of entries
const uint_t c_uNumberOfIdtEntries = 0x100;

typedef struct GdtPointer {
    DECL_PACKED uint16_t u16Limit;
    DECL_PACKED uint32_t u32Base;
} GdtPointer;
typedef struct DECL_PACKED GateDescriptor {
    uint16_t    u16Offset_0_15;
    uint16_t    u16SegmentSelector;
    uint16_t    u16Flag;
    uint16_t    u16Offset_16_31;
} GateDescriptor;

EXTERN_C void CDECL TestKSetup1();
EXTERN_C void CDECL TestSegment1();
EXTERN_C void CDECL TestBootModule1();
EXTERN_C void CDECL TestInterrupt1();

INLINE uint32_t GetCs()
{
    uint32_t r;
    ASM("movl   %%cs, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetDs()
{
    uint32_t r;
    ASM("movl   %%ds, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetEs()
{
    uint32_t r;
    ASM("movl   %%es, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetFs()
{
    uint32_t r;
    ASM("movl   %%fs, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetSs()
{
    uint32_t r;
    ASM("movl   %%ss, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetCr0()
{
    uint32_t r;
    ASM("movl   %%cr0, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetCr3()
{
    uint32_t r;
    ASM("movl   %%cr3, %%eax;"
        :"=a"(r));
    return r;
}

INLINE uint32_t GetCr4()
{
    uint32_t r;
    ASM("movl   %%cr4, %%eax;"
        :"=a"(r));
    return r;
}

INLINE void TestPaging1()
{
    kprintf("======\n");
    kprintf("cs is %x\n", GetCs());
    kprintf("ds is %x\n", GetDs());
    kprintf("es is %x\n", GetEs());
    kprintf("fs is %x\n", GetFs());
    kprintf("ss is %x\n", GetSs());

    kprintf("======\n");
    kprintf("Cr0 is %x\n", GetCr0());
    kprintf("Cr3 is %x\n", GetCr3());
    kprintf("Cr4 is %x\n", GetCr4());
}

EXTERN GateDescriptor g_idt[c_uNumberOfIdtEntries];
EXTERN ipl_t g_currentIpl;
EXTERN irqs_t g_irqsTable[];

#endif //__ELASTOS_TESTX86_H__
